﻿using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.UI;
using System.Xml;
using System.Xml.Linq;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_General_RSS_RSS : WebModule
{
    protected override void WebModuleRender(HtmlTextWriter output)
    {
        int count = WebHelpers.Int32ParseSafe((string)Settings["Count"], 3);
        if (count == 0)
            return;
        XElement feed = null;
        if (string.IsNullOrEmpty((string)Settings["Url"]))
            return;
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create((string)Settings["Url"]);
            using (WebResponse response = request.GetResponse())
            {
                using (XmlTextReader reader = new XmlTextReader(new StreamReader(response.GetResponseStream(), true)))
                {
                    feed = XElement.Load(reader);
                }
            }
        }
        catch
        {
            return;
        }
        List<string> text = new List<string>();
        List<string> links = new List<string>();
        if (feed.Element("channel") != null)
        {
            foreach (XElement item in feed.Element("channel").Elements("item"))
            {
                string Title = item.Element("title").Value;
                string Link = item.Element("link").Value;
                if (string.IsNullOrEmpty(Title))
                    continue;
                if (string.IsNullOrEmpty(Link))
                    continue;
                text.Add(Title);
                links.Add(Link);
                if (text.Count == count)
                    break;
            }
        }
        if (text.Count == 0)
            return;        
        string data = WebHelpers.GenerateBulletedListHyperLinks(text.ToArray(), links.ToArray());
        output.Write(data);
    }    
}
